//
//  DesignerSignForNewStcFormVC.swift
//  ezyform
//
//  Created by Moennig Q on 7/5/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
class DesignerSignForNewStcFormVC: BaseViewController {
    weak var model: PVDDetailModel!
    let customView = DesignerSignForNewStcFormView(customBackToPreFlag: true)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.model = JobDetailModelHelper.shared.jobDetailModel
        initUI()
    }
    func initUI(){
        //view
        self.view = self.customView
        self.customView.backToPreBtn.addTarget(self, action: #selector(backToPreVieController), for: UIControl.Event.touchUpInside)
        self.customView.setTitleInfo(APPConfig.loginInfo.currentSignatureType.rawValue + " Signature")
        
        self.customView.setData(model.signatures.designer_full_name)
        self.customView.selectWitnessView.selectWitnessClosure = { [weak self]  in
            guard let strongSelf = self else { return  }
            //检查是否已签名
            if !strongSelf.customView.signView.signaturedFlag {
                Tool().showAlertViewWithOneBtn(message: R.string.localizable.pleaseSignBeforeSaving())
                return
            }
            strongSelf.saveSignature()
        }
    }
    @objc func backToPreVieController() {
        Tool().showAlertViewWithTwoBtns(message: R.string.localizable.pvdDetailSignaturesSignatureBackfromsignTip(),  yesClosure: {[weak self] in
            self?.navigationController?.popViewController(animated: false)
        })
    }
    func saveSignature(){
        let image = self.customView.signView.getSignature()
        
        if let imageData = image.pngData() {
            APPConfig.loginInfo.signatureInfoModel.pvd_id = JobDetailModelHelper.shared.jobDetailModel.id
            //签名图片
            APPConfig.loginInfo.signatureInfoModel.designer_sign_imagedata = NSData(data: imageData)
            APPConfig.loginInfo.signatureInfoModel.designer_signed_at = DateUtil().getCurrentDatetime()
            if self.customView.selectWitnessView.ownerAsWitnessFlag{//选择owner作为witness
                let vc = OnlineStatementViewController(statementType: .Owner)
                self.navigationController?.pushViewController(vc, animated: false)
            }else{//other作为witness
                let vc = WitnessSignForNewStcFormVC()
                self.navigationController?.pushViewController(vc, animated: false)
            }
        }else{
            Tool().showAlertViewWithOneBtn(message: NSLocalizedString("pvd.detail.signatures.signature.save.faild", comment: ""))
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        DebugLog("\(self) didReceiveMemoryWarning")
    }

}
